﻿using STHPMS1.Common.C_Entity;
using STHPMS1.Common.C_Entity.Stmpms1;
using STHPMS1.ICore;
using STHPMS1.ICore.I_Core;
using STHPMS1.ICore.I_UI;
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace STHPMS1.Web.W_Controller
{
    [RoutePrefix("api/pmds_s")]
    public class S_Pmds_SPage1Controller : STHPMS1.Web.W_Core.MyController
    {
        [Route("")]
        [Route("page/{page?}", Name = "Getpmds_sByPage")]
        [Route("rows/{rows?}")]
        [Route("page/{page?}/rows/{rows?}")]
		public IHttpActionResult Get(string road = null,string inspecid=null, DateTime? checkDate = null, int page = 1, int rows = 10)
        {
            int p = page;
            int s = rows;

            PagingInfo info = new PagingInfo();
            info.PageIndex = p > 0 ? p - 1 : 0;
            info.PageSize = s;
            info.TotalRecords = 0;

            Expression<Func<S_Pmds_S, bool>> exp = null;
            if (((road != null && road.ToString().Length > 0)) && (checkDate != null) && ((inspecid != null && inspecid.ToString().Length > 0)))
            {
                exp = pre => (pre.Rdwayid == road.ToString()) && (pre.Inspecid == inspecid.ToString()) && (pre.Svydata == checkDate);
            }
            else if (((road != null && road.ToString().Length > 0)) &&  ((inspecid != null && inspecid.ToString().Length > 0)))
            {
                exp = pre => (pre.Rdwayid == road) && (pre.Inspecid == inspecid.ToString());
            }
            else if ((road != null && road.ToString().Length > 0) && (checkDate != null) )
            {
                exp = pre => (pre.Rdwayid == road.ToString()) && (pre.Svydata == checkDate);
            }
            else if ((checkDate != null) && (inspecid != null && inspecid.ToString().Length > 0))
            {
                exp = pre => (pre.Inspecid == inspecid.ToString()) && (pre.Svydata == checkDate);
            }
            else if (road != null && road.ToString().Length > 0)
            {
                exp = pre => pre.Rdwayid == road.ToString();
            }
            else if (inspecid != null && inspecid.ToString().Length > 0)
            {
                exp = pre => pre.Inspecid == inspecid.ToString();
            }
            else if (checkDate != null)
            {
                exp = pre => pre.Svydata == checkDate;
            }


            var data = this.Scene.Bll.S_Pmds_S.Get<S_Pmds_S>(exp, info).ToArray();
            if (data == null) return NotFound();

            var result = new { rows = data, total = info.TotalRecords };

            return Ok(result);
        }

        [Route("")]
        public HttpResponseMessage Post(S_Pmds_S entity)
        {
            entity.Importdate = DateTime.Now;
            this.Scene.Bll.S_Pmds_S.Save(entity);

            var response = Request.CreateResponse(HttpStatusCode.Created);
            // Generate a link to the new book and set the Location header in the response.
            string uri = Url.Link("Getpmds_sByPage", new { page = 1 });
            response.Headers.Location = new Uri(uri);

            return response;
        }

        [Route("")]
        public void Delete(S_Pmds_S entity)
        {
            this.Scene.Bll.S_Pmds_S.Remove(entity);
        }
    }
}